package org.example.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {


    @PostMapping("/login")
    public String login(@RequestParam String id, @RequestParam String passwd,
                        Model model, HttpServletRequest request){

        if("123".equals(passwd)){
            request.getSession().setAttribute("id",id);
            model.addAttribute("loginId",id);
            return "main";
        }
        return "login";
    }




    @GetMapping("/tologin")
    public String tologin(){

        return "login";
    }



    @GetMapping("/getSession")
    @ResponseBody
    public String getSession(HttpServletRequest request){


        HttpSession session = request.getSession();
        if(session ==null) return "没有创建Session。";
        String id=(String) session.getAttribute("id");
        if(id==null) return "没有查询到您的id信息";
        return "查询到您登录的id是："+id;

    }


}
